package com.yl.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Users;
import com.yl.auth.entity.YuangongBumen;
import com.yl.auth.entity.YuangongGangwei;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 * 员工
 *
 * @author
 * @since 2023-06-15
 */
@RestController
@RequestMapping("/admin/auth/yuangong")
@Api(tags = "人事行政-员工管理")
public class YuangongController {

    @Autowired
    private YuangongService yuangongService;

    @Autowired
    private YuangongBumenService ybservice;

    @Autowired
    private YuangongGangweiService ygservice;
    @Autowired
    private UsersService usersService;

    @Autowired
    private LaorenService laorenService;
    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param yuangong
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, Yuangong yuangong) {
        Page<Yuangong> absentPage = new Page<>(page, limit);
        IPage<Yuangong> page1 = yuangongService.getygList(absentPage, yuangong);
        return Result.ok(page1);
    }

    /**
     * 修改信息
     *
     * @param yuangong
     * @return
     */
    @ApiOperation("修改信息")
    @PutMapping("/update")
    public Result update(@RequestBody Yuangong yuangong, @RequestParam(value = "bumenList") List<Integer> bumenList, @RequestParam(value = "gangweiList") List<Integer> gangweiList) {
        yuangongService.updateUsers(yuangong);
        ybservice.updateBuMen(yuangong.getId(), bumenList);
        ybservice.updateGangWei(yuangong.getId(), gangweiList);
        return Result.ok("成功");
    }

    @PostMapping("/addYuanGong")
    @ApiOperation(value = "添加员工")
    public Result addLaoRenAndBingShi(@RequestBody Yuangong yuangong, @RequestParam(value = "bumenList") List<Integer> bumenList, @RequestParam(value = "gangweiList") List<Integer> gangweiList) {
        usersService.saveYuangong(yuangong);
        yuangongService.addXinzi(yuangong);
        ybservice.addBuMen(yuangong.getId(), bumenList);
        ybservice.addGangWei(yuangong.getId(), gangweiList);
        return Result.ok();
    }

    @ApiOperation("查询所有的老人")
    @GetMapping("/getAlllaoren")
    public Result getAlllaoren(@RequestParam Integer id){
        System.out.println(id+"=====================================");
        List<Laoren> list = laorenService.listlaoren(id);
        return Result.ok(list);
    }
    @ApiOperation("分配老人")
    @PostMapping("/savelaoren/{id}")
    public Result savelaoren(@RequestBody List<Integer> ids,@PathVariable Integer id){
        Yuangong byId = yuangongService.getById(id);
        for (Integer id1 : ids) {
            yuangongService.savelaoren(id1,byId.getId());
        }
        return Result.ok();
    }
    /**
     * 删除信息
     *
     * @param id
     * @return
     */
    @ApiOperation("删除信息")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Long id,Yuangong yuangong) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("yuangongid", id);
        //删除员工部门中间表
        ybservice.remove(queryWrapper);
        //删除员工岗位中间表
        usersService.removeRole(yuangong);
        //删除员工薪资和账号
        yuangongService.deleteById(id,yuangong);
        yuangongService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id查询信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询信息")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id) {
        Yuangong yuangong = yuangongService.getByID(id);
        return Result.ok(yuangong);
    }

    @ApiOperation("点击按钮停用")
    @PostMapping("/enable/{id}")
    public Result enable(@PathVariable Long id) {
        Yuangong yuangong = new Yuangong();
        yuangong.setId(id);
        yuangong.setStatus(1);
        yuangongService.updateById(yuangong);
        return Result.ok();
    }

    @ApiOperation("点击按钮启用")
    @PostMapping("/stop/{id}")
    public Result stop(@PathVariable Long id) {
        Yuangong yuangong = new Yuangong();
        yuangong.setId(id);
        yuangong.setStatus(0);
        yuangongService.updateById(yuangong);
        return Result.ok();
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    /*图片上传上传图片接口*/
    /*图片上传地址*/
    String fileSavePath = "D:/Workspace/shixun/ylHouse/codeylhomes/public/upload/";
    /*格式化时间*/
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");

    @PostMapping("/saveUpload")
    @ApiOperation(value = "图片上传")
    @CrossOrigin
    public Result uploadFile(@ApiParam(name = "file", value = "上传", required = true) @RequestPart("file") MultipartFile file, HttpServletRequest request) {
        /*将时间转化成字符串*/
        Date date = new Date();
        String directory = simpleDateFormat.format(date);
        /*文件路径是否存在
         * 如果不存在,则创建
         */
        File dir = new File(fileSavePath + directory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        /*获取图片的名字*/
        String imageName = file.getOriginalFilename();
        System.out.println("图片上传位置" + fileSavePath + directory);
        /*给文件创建一个新的名字*/
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        /*图片新的名字*/
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        System.out.println("上传的图片位置" + fileSavePath + directory + newFileName);
        /*创建新文件*/
        File newFile = new File(fileSavePath + directory + newFileName);
        try {
            file.transferTo(newFile);
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + directory + newFileName;
            System.out.println("图片上传成功的url:" + url);
            String sql = "/upload/" + directory + newFileName;
            /*返回给前端的字符串地址,然后添加的时候将这一串字符串加入到数据库中*/
            return Result.ok(sql);
        } catch (IOException e) {
            return Result.ok();
        }
    }

    @ApiOperation("条件分页查询护工")
    @GetMapping("/listhugong/{page}/{limit}")
    public Result listhugong(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, Yuangong yuangong) {
        Page<Yuangong> page2 = yuangongService.getAllAndPage(page, limit, yuangong);
        return Result.ok(page2);
    }

    @ApiOperation("根据员工id得到他的部门回显")
    @GetMapping("/getBuMenById/{id}")
    public Result listhugong(@PathVariable("id") Integer id) {
        List<Integer> bumen = yuangongService.getBuMenById(id);
        return Result.ok(bumen);
    }

    @ApiOperation("根据员工id得到他的岗位回显")
    @PostMapping("/getGangWeiById/{id}")
    public Result getGangWeiById(@PathVariable Integer id) {
        List<Integer> gangwei = yuangongService.getGangWeiById(id);
        return Result.ok(gangwei);
    }
    @ApiOperation("修改登录之后的账号的密码")
    @PutMapping("/updatePassWord")
    public Result updatePassWord(@RequestBody Yuangong yuangong) {
        yuangongService.updatePassWord(yuangong);
        return Result.ok();
    }
    private static Yuangong staff = new Yuangong();

    @ApiOperation(value = "回显登录账号")
    @GetMapping("/huixianzhanghaoxinxi")
    public Result huixianzhanghaoxinxi(){
        return Result.ok(staff);
    }
    @ApiOperation("根据员工id")
    @GetMapping("/getbyId/{id}")
    public Result getbyId(@PathVariable("id") Integer id) {
        Yuangong byId = yuangongService.getById(id);
        return Result.ok(byId);
    }
    @ApiOperation(value = "登录小程序")
    @PostMapping("/login6")
    public Result login3(@RequestParam("account")String account,@RequestParam("password")String password){
        QueryWrapper<Yuangong> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("password",password);
        Yuangong yuangong = yuangongService.getOne(queryWrapper);
        if(yuangong!=null){
            return Result.ok(200);
        }else{
            return Result.ok(500);
        }
    }
}
